package org.mineground.commands.player;

import java.util.ArrayList;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.mineground.Main;
import org.mineground.Utilities;

/**
 * @file GroupChat.java (20.02.2012)
 * @author Daniel Koenen
 *
 */
public class GroupChat implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] arguments) {
        Player player = (Player) cs;
        
        if (arguments.length < 1) {
            player.sendMessage(ChatColor.RED + "* Usage: '/groupchat <List/Create/Join/Members>'");
	    return true;
	}
        
        if (arguments[0].equalsIgnoreCase("list")) {
            int pageNumber = 1;
            
            if (arguments.length > 1 && Utilities.isNumeric(arguments[1])) {
                pageNumber = Integer.parseInt(arguments[1]);
            }
            
            if (pageNumber < 0 || pageNumber > Main.getInstance().getGroupChatHandler().getTotalPages()) {
                player.sendMessage(ChatColor.RED + "* Error: Invalid page number.");
                return true;
            }
            
            ArrayList<org.mineground.player.groupchat.GroupChat> groupChatList = Main.getInstance().getGroupChatHandler().getGroupChatList(pageNumber);
            
            for (org.mineground.player.groupchat.GroupChat groupChat : groupChatList) {
                player.sendMessage(ChatColor.GOLD + "#" + groupChat.getGroupId() + " -> " + groupChat.getGroupName());
            }
            
            player.sendMessage(ChatColor.DARK_AQUA + "Page " + pageNumber + " of " + Main.getInstance().getGroupChatHandler().getTotalPages());
            return true;
        }
        
        else if (arguments[0].equalsIgnoreCase("create")) {
            long password = 0;
            String groupName;
            
            if (arguments.length < 2) {
                player.sendMessage(ChatColor.RED + "* Usage: '/groupchat Create <GroupName> (<GroupPassword>)'");
                return true;
            }
            
            if (arguments.length == 3) {
                password = arguments[2].hashCode();
            }
            
            groupName = arguments[1];
            
            if (!groupName.matches("^[0-9_a-zA-Z]*$") || groupName.length() > 20) {
		player.sendMessage(ChatColor.RED + "* Error: Only letters and _ allowed (Max. Length 20).");
		return true;
	    }
            
            int oldGroupId = Main.getInstance().getPlayer(player).getGroupChatId();
            if (oldGroupId != -1) {
                if (Main.getInstance().getGroupChatHandler().doesGroupExist(oldGroupId)) {
                    Main.getInstance().getGroupChatHandler().getGroupChat(oldGroupId).removePlayer(player);
                }
            }
            
            int groupId = Main.getInstance().getGroupChatHandler().createGroupChat(groupName, password);
            Main.getInstance().getPlayer(player).setGroupChatId(groupId);
            Main.getInstance().getGroupChatHandler().getGroupChat(groupId).addPlayer(player);
            
            player.sendMessage(ChatColor.DARK_GREEN + "* You successfuly created a new chat group. Use '#' to chat in it.");
            Main.getInstance().getIRCHandler().sendAdminMessage(Colors.BROWN + Utilities.fixName(player) + Colors.BROWN + " created a new chat group. (Name: " + groupName + ")");
	    return true;
        }
        
        else if (arguments[0].equalsIgnoreCase("join")) {
            long password = 0;
            int groupId;
            
            if (arguments.length < 2) {
                player.sendMessage(ChatColor.RED + "* Usage: '/groupchat Join <GroupId> (<GroupPassword>)'");
                return true;
            }
            
            if (!Utilities.isNumeric(arguments[1])) {
                player.sendMessage(ChatColor.RED + "* Usage: '/groupchat Join <GroupId> (<GroupPassword>)'");
                return true;
            }
            
            groupId = Integer.parseInt(arguments[1]);
            
            if (!Main.getInstance().getGroupChatHandler().doesGroupExist(groupId)) {
                player.sendMessage(ChatColor.RED + "* Error: Invalid group id.");
                return true;
            }
            
            org.mineground.player.groupchat.GroupChat groupChat = Main.getInstance().getGroupChatHandler().getGroupChat(groupId);
            
            if (groupChat.getGroupPassword() != 0) {
                if (arguments.length < 3) {
                    player.sendMessage(ChatColor.RED + "* Error: Invalid password.");
                    return true;
                }
                
                password = arguments[2].hashCode();
            }
            
            if (password != groupChat.getGroupPassword()) {
                player.sendMessage(ChatColor.RED + "* Error: Invalid password.");
                return true;
            }
            
            int oldGroupId = Main.getInstance().getPlayer(player).getGroupChatId();
            if (oldGroupId != -1) {
                if (Main.getInstance().getGroupChatHandler().doesGroupExist(oldGroupId)) {
                    Main.getInstance().getGroupChatHandler().getGroupChat(oldGroupId).removePlayer(player);
                }
            }
            
            Main.getInstance().getPlayer(player).setGroupChatId(groupId);
            Main.getInstance().getGroupChatHandler().getGroupChat(groupId).addPlayer(player);
            
            for (Player loopPlayer : Main.getInstance().getGroupChatHandler().getGroupChat(groupId).getGroupPlayers()) {
                loopPlayer.sendMessage(ChatColor.DARK_AQUA + "[" + Main.getInstance().getGroupChatHandler().getGroupChat(groupId).getGroupName() + "] " + player.getDisplayName() + " joined the chat.");
            }
            
            player.sendMessage(ChatColor.GOLD + "Use '#' as chat-prefix.");
            Main.getInstance().getIRCHandler().sendAdminMessage(Colors.BROWN + Utilities.fixName(player) + Colors.BROWN + " joined " + Main.getInstance().getGroupChatHandler().getGroupChat(groupId).getGroupName() + ".");
	    return true;
        }
        
        else if (arguments[0].equalsIgnoreCase("members")) {
            int pageNumber = 1;
            
            if (arguments.length > 1 && Utilities.isNumeric(arguments[1])) {
                pageNumber = Integer.parseInt(arguments[1]);
            }
            
            if (Main.getInstance().getPlayer(player).getGroupChatId() == -1) {
                player.sendMessage(ChatColor.RED + "* Error: Please join a group first.");
                return true;
            }
            
            int groupId = Main.getInstance().getPlayer(player).getGroupChatId();
            ArrayList<Player> pagePlayers = Main.getInstance().getGroupChatHandler().getGroupChat(groupId).getMemberPage(pageNumber);
            
            if (pageNumber < 0 || pageNumber > Main.getInstance().getGroupChatHandler().getGroupChat(groupId).getTotalPages()) {
                player.sendMessage(ChatColor.RED + "* Error: Invalid page number.");
                return true;
            }
            
            for (Player loopPlayers : pagePlayers) {
                player.sendMessage(ChatColor.GOLD + loopPlayers.getName());
            }
            
            player.sendMessage(ChatColor.DARK_AQUA + " Page " + pageNumber + " of " + Main.getInstance().getGroupChatHandler().getGroupChat(groupId).getTotalPages());
            return true;
        }
        
        else {
            player.sendMessage(ChatColor.RED + "* Usage: '/groupchat <List/Create/Join/Members>'");
	    return true;
	}
    }
}
